package com.gl.glcourses.mail.impl;

import java.util.ArrayList;
import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;
import org.springframework.ui.velocity.VelocityEngineUtils;

import com.gl.glcourses.vos.Usuario;
import com.gl.glcourses.vos.Votacion;
import com.gl.glcourses.vos.constants.Constants;

@Component
public class SendCrearVotacion extends VelocityEmailSender{
	
	@Autowired
	public SendCrearVotacion(VelocityEngine velocityEngine,
			JavaMailSender mailSender) {
		super(velocityEngine, mailSender);
	}

	public void send(final SimpleMailMessage msg, 
                final Map<Object, Object> hTemplateVariables) {
		
		   MimeMessagePreparator preparator = new MimeMessagePreparator() {
		   
	       public void prepare(MimeMessage mimeMessage) throws Exception {

	    	  MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
	          
	          message.setTo( getTo( hTemplateVariables ) );
	          message.setFrom( getFrom() );
	          message.setSubject(msg.getSubject());
	          hTemplateVariables.put("url", appUrl+"/index.jsp?vista=votacion&estado=ESTADO_VOTAR&votacionId="+getVotacionId(hTemplateVariables)); 
	          String body = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "templates/templateAbrirVotacion.vm", hTemplateVariables);
	          message.setText(body, true);
	         
	       }
	   };
	   execute( preparator );

   }
	
	 private long getVotacionId( final Map<Object, Object> hTemplateVariables )
	 {
			Votacion votacion = (Votacion) hTemplateVariables.get("votacion");
			return votacion.getId();
	 }


}
